﻿using Essensoft.Paylink.WeChatPay;
using Essensoft.Paylink.WeChatPay.V3;
using Essensoft.Paylink.WeChatPay.V3.Notify;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using Nucleic.Core.DoMain;
using Nucleic.Core.Utils;

namespace Nucleic.Web.Controllers
{
    [Route("wechatpay/v3/notify")]
    public class WeChatPayV3NotifyController : Controller
    {
        private readonly ILogger<WeChatPayV3NotifyController> _logger;
        private readonly IWeChatPayNotifyClient _client;
        private readonly IOptions<WeChatPayOptions> _optionsAccessor;

        public WeChatPayV3NotifyController(ILogger<WeChatPayV3NotifyController> logger, IWeChatPayNotifyClient client, IOptions<WeChatPayOptions> optionsAccessor)
        {
            _logger = logger;
            _client = client;
            _optionsAccessor = optionsAccessor;
        }
        [HttpGet]

        public IActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 支付结果通知
        /// </summary>
        [Route("transactions")]
        [HttpPost]
        public async Task<IActionResult> Transactions()
        {
            try
            {
                
                var notify = await _client.ExecuteAsync<WeChatPayTransactionsNotify>(Request, _optionsAccessor.Value);
                //在ASP.NET Core中Request Body是Stream的形式
                StreamReader stream = new StreamReader(Request.HttpContext.Request.Body);
                string body = stream.ReadToEnd();
                _logger.LogInformation("微信支付通知 => : " + body);
                if (notify.TradeState == WeChatPayTradeState.Success)
                {
                    _logger.LogInformation("支付结果通知 => OutTradeNo: " + notify.OutTradeNo);
                    var book = SqlSugarFactory.Instance.Queryable<book_info>().First(it => it.orders_id == notify.OutTradeNo);
                    if (book != null)
                    {
                        if (book.status == "0")
                        {
                            book.status = "1";
                            book.update_time = DateTime.Now;
                            SqlSugarFactory.Instance.Updateable(book).ExecuteCommand();
                        }

                        var item = SqlSugarFactory.Instance.Queryable<check_item>().First(i=>i.item_id==book.item_id);
                        var itemtype= SqlSugarFactory.Instance.Queryable<check_type>().First(i => i.item_id == book.item_id);
                        order_info order_ = new order_info
                        {
                            pay_time = DateTime.Now,
                            pat_id = book.pat_id,
                            appo_id = book.appo_id,
                            item_id = book.item_id,
                            price=item.price,
                            num= itemtype.num,
                            orders_id = book.orders_id,
                            wx_order_no = notify.TransactionId,
                            status = "0"
                        };
                        SqlSugarFactory.Instance.Insertable<order_info>(order_).ExecuteCommand();
                    }
                   
                    
                    return WeChatPayNotifyResult.Success;
                }

                return WeChatPayNotifyResult.Failure;
            }
            catch (WeChatPayException ex)
            {
                _logger.LogWarning("出现异常: " + ex.Message);
                return WeChatPayNotifyResult.Failure;
            }
        }
    }
}
